{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 注意！请根据提示操作！\n",
    "\n",
    "如果当页面加载完成，你还是看到了这条提示\n",
    "\n",
    "那么你的notebook并未被信任，导致代码无法正常运行\n",
    "\n",
    "点击右上角的`不可信`，将本notebook列入可信列表，即可正常运行。\n",
    "\n",
    "![可信按钮](./mgr/trust.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 变量的类型\n",
    "变量有几种基础类型，分别为：\n",
    "\n",
    "### 整型变量(int)\n",
    "也就是整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "print(a)\n",
    "print(type(a)) # type()函数可以获取某个变量的类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 浮点数变量(float)\n",
    "也就是小数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1.141\n",
    "print(a, type(a)) # 可以一口气print多个不同的值，值与值之间用逗号隔开。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔变量(bool)\n",
    "布尔变量只有两个值，True或者False，分别代表的是真和假。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = True\n",
    "print(a, type(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串变量(string)\n",
    "也就是文字，用引号引起来的。\n",
    "\n",
    "在python中，双引号和单引号都可以表示字符串，一般没什么区别。\n",
    "\n",
    "但建议保持一种风格，不要单双引号混用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'HelloWorld'\n",
    "print(a, type(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意字符串中出现的阿拉伯数字，与整型不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = '32'\n",
    "print(a / 4) # 此处将会报错，因为字符串无法用于数值计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但是字符串可以用加号和乘号，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'Hello'\n",
    "b = 'World'\n",
    "print(a + b) # 拼接两个字符串\n",
    "print(a * 3) # 重复3次a字符串的拼接"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 变量间的转换\n",
    "变量类型间可以互相转换，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1.145 # 浮点数类型\n",
    "print(a, type(a))\n",
    "\n",
    "b = int(a) # 转化成整数，小数部分会自动舍去\n",
    "print(b, type(b))\n",
    "\n",
    "c = str(b) # 转换成字符串，不会有数据丢失\n",
    "print(c, type(c))\n",
    "\n",
    "d = float(c) # 转化成浮点数，由于b = int(a)转换时的小数丢失，导致最后转换回去后，失去了小数部分的数据\n",
    "print(d, type(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "观察下列代码，猜测下运行后的输出，看看是否真的理解了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "b = 2\n",
    "print(a + b)\n",
    "print(str(a) + str(b))\n",
    "print(a * b)\n",
    "print(str(a) * b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = '1'\n",
    "b = '2'\n",
    "print(a + b)\n",
    "print(a * int(b))\n",
    "print(b * int(a))\n",
    "print(int(b) * int(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = '1'\n",
    "b = 2\n",
    "# print(a + b) # 此行代码直接运行会报错，请尝试修改后再运行\n",
    "print(a * b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<hr>\n",
       "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
       "<script src=\"./mgr/submit.js\"></script>\n",
       "<script src=\"./mgr/hide.js\"></script>\n",
       "<div class=\"mdui-container-fluid\">\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
       "    </div>\n",
       "</div>\n",
       "<div id=\"html_output\">\n",
       "</div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<hr>\n",
    "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
    "<script src=\"./mgr/submit.js\"></script>\n",
    "<script src=\"./mgr/hide.js\"></script>\n",
    "<div class=\"mdui-container-fluid\">\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
    "    </div>\n",
    "</div>\n",
    "<div id=\"html_output\">\n",
    "</div>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
